import Vue from 'vue'
import VueRouter from 'vue-router'
const Index = () => import('../views/index/Index');
const Home = () => import('../views/Home/Home');
const Login = () => import('../views/login/Login');
const HcjNote = () => import('../views/studyNote/htmlCssJs/HcjNote');
const Node = () => import('../views/studyNote/nodeJs/Node');
const VueNote = () => import('../views/studyNote/vueJs/VueNote');
const LeaveMess = () => import('../views/leaveMess/LeaveMess');
const Article = () => import('../views/article/Article');
Vue.use(VueRouter);

  const routes = [
    {path: '/',redirect:'/index'},
    {path: '/index',component:Index},
    {path:'/home',component:Home,
      children:[
          {path:'/',redirect:'/hcj'},
        {path: '/hcj',component:HcjNote},
        {path: '/node',component:Node},
        {path: '/vueNote',component:VueNote},
        {path: '/leaveMess',component:LeaveMess},
        {path: '/article',component:Article},
        ]},
    {path:'/login',component:Login}
];
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

//解决element-ui重复点击菜单，路由报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
};

//挂载路由导航守卫
router.beforeEach((to,from,next) => {
  if (to.path === '/login' || to.path === '/index' || to.path === '/hcj'){
    return next();
  }
  const tokenStr = sessionStorage.getItem('session');
  if(!tokenStr){
    return next('/login');
  }
  next();
});


export default router
